package com.ysstech.reportworld.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ysstech.common.controller.BaseController;
import com.ysstech.common.entity.Dicvalue;
import com.ysstech.common.util.resultutil.RestResult;
import com.ysstech.common.util.resultutil.ResultGenerator;
import com.ysstech.reportworld.entity.DataSql;
import com.ysstech.reportworld.entity.SqlScript;
import com.ysstech.reportworld.entity.Table;
import com.ysstech.reportworld.enums.ReportWorldEnum;
import com.ysstech.reportworld.service.*;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 报表世界数据平台首页统计项
 */
@Controller
@Slf4j
@Api(tags = "平台首页统计项管理api")
@RequestMapping("/statisticsReportWorld")
public class StatisticsReportWorldController extends BaseController {
    @Autowired
    private TableService tableService;
    @Autowired
    private DataSqlService dataSqlService;
    @Autowired
    public DemandBugInfoService demandBugInfoService;
    @Autowired
    private SqlScriptService sqlScriptService;
    @Value("${file.uploadFolder}")
    private String uploadFolder;


    /**
     * 报表世界统计信息展示到首页
     *
     * @throws Exception
     */
    @RequestMapping(value = "/statisticsReportWorld")
    @ResponseBody
    public RestResult statisticsReportWorld() throws Exception {
        Map<String, Object> map = new HashMap<>();
        map.put("sql_script", this.getSqlScriptMsg());
        map.put("report_wrold_table", this.getReportWorldTableMsg());
        map.put("report_wrold_sql", this.getReportWorldSqlMsg());
        // 占时不统计
        //  map.put("report_wrold_base_data", this.getReportBaseDataSqlMsg());
        return ResultGenerator.getSuccessResult("生成升级所需的文件信息成功！", map);
    }

    /**
     * 报表世界基础数据 条数
     *
     * @return
     * @throws Exception
     */
    private Map<String, Object> getReportBaseDataSqlMsg() throws Exception {
        Map<String, Object> map = new HashMap<>();
        //状态
        Map<String, String> mapStatus = dicValueService.getCacheDicValueMap(ReportWorldEnum.REPORT_STATUS.getCode());
        if (CollectionUtils.isEmpty(mapStatus)) {
            return null;
        }
        List<Dicvalue> dicvalueList = this.getVersionList();
        if (CollectionUtils.isEmpty(dicvalueList)) {
            return null;
        }
        List<String> categories = new ArrayList<>(); // 所有的状态类型
        for (Dicvalue dicvalue : dicvalueList) {  // 获取每个版本的数据信息
            if (null == dicvalue) {
                continue;
            }
            categories.add(dicvalue.getValue());
        }
        List<Map<String, Object>> list = new ArrayList<>();
        for (Map.Entry<String, String> entry : mapStatus.entrySet()) {
            Map<String, Object> series = new HashMap<>(); //  所有状态
            List<Integer> integerList = new ArrayList<>(); //每个状态对应的个数集合
            for (Dicvalue dicvalue : dicvalueList) {  // 获取每个版本的数据信息
                if (null == dicvalue) {
                    continue;
                }
            }
            series.put("data", integerList);
            series.put("name", entry.getValue());
            list.add(series);
        }
        map.put("series", list);
        map.put("categories", categories);
        return map;
    }


    /**
     * 报表世界表结构，脚本，基础数据 条数
     *
     * @return
     * @throws Exception
     */
    private Map<String, Object> getReportWorldSqlMsg() throws Exception {
        Map<String, Object> map = new HashMap<>();
        //状态
        Map<String, String> mapStatus = dicValueService.getCacheDicValueMap(ReportWorldEnum.REPORT_STATUS.getCode());
        if (CollectionUtils.isEmpty(mapStatus)) {
            return null;
        }
        List<Dicvalue> dicvalueList = this.getVersionList();
        if (CollectionUtils.isEmpty(dicvalueList)) {
            return null;
        }
        List<String> categories = new ArrayList<>(); // 所有的状态类型
        for (Dicvalue dicvalue : dicvalueList) {  // 获取每个版本的数据信息
            if (null == dicvalue) {
                continue;
            }
            categories.add(dicvalue.getValue());
        }
        List<Map<String, Object>> list = new ArrayList<>();
        for (Map.Entry<String, String> entry : mapStatus.entrySet()) {
            Map<String, Object> series = new HashMap<>(); //  所有状态
            List<Integer> integerList = new ArrayList<>(); //每个状态对应的个数集合
            for (Dicvalue dicvalue : dicvalueList) {  // 获取每个版本的数据信息
                if (null == dicvalue) {
                    continue;
                }
                QueryWrapper<DataSql> wrapperDataSql = new QueryWrapper<>();
                wrapperDataSql.eq("VERSION", dicvalue.getValue());
                wrapperDataSql.eq("STATUS", entry.getKey());
                wrapperDataSql.groupBy("VERSION,STATUS");
                wrapperDataSql.select("count(1) AS COUNT,STATUS");
                wrapperDataSql.orderByAsc("STATUS");
                List<DataSql> dataSqlList = dataSqlService.list(wrapperDataSql);
                if (CollectionUtils.isEmpty(dataSqlList)) {
                    integerList.add(0);
                    continue;
                }
                for (DataSql table : dataSqlList) {
                    if (null == dataSqlList) {
                        integerList.add(0);
                        continue;
                    }
                    integerList.add(table.getCount());
                }
            }
            series.put("data", integerList);
            series.put("name", entry.getValue());
            list.add(series);
        }
        map.put("series", list);
        map.put("categories", categories);
        return map;
    }

    /**
     * 报表世界表结构
     *
     * @return
     * @throws Exception
     */
    private Map<String, Object> getReportWorldTableMsg() throws Exception {
        Map<String, Object> map = new HashMap<>();
        //状态
        Map<String, String> mapStatus = dicValueService.getCacheDicValueMap(ReportWorldEnum.REPORT_STATUS.getCode());
        if (CollectionUtils.isEmpty(mapStatus)) {
            return null;
        }
        List<Dicvalue> dicvalueList = this.getVersionList();
        if (CollectionUtils.isEmpty(dicvalueList)) {
            return null;
        }
        List<String> categories = new ArrayList<>(); // 所有的状态类型
        for (Dicvalue dicvalue : dicvalueList) {  // 获取每个版本的数据信息
            if (null == dicvalue) {
                continue;
            }
            categories.add(dicvalue.getValue());
        }
        List<Map<String, Object>> list = new ArrayList<>();
        for (Map.Entry<String, String> entry : mapStatus.entrySet()) {
            Map<String, Object> series = new HashMap<>(); //  所有状态
            List<Integer> integerList = new ArrayList<>(); //每个状态对应的个数集合
            for (Dicvalue dicvalue : dicvalueList) {  // 获取每个版本的数据信息
                if (null == dicvalue) {
                    continue;
                }
                QueryWrapper<Table> wrapperTable = new QueryWrapper<>();
                wrapperTable.eq("VERSION", dicvalue.getValue());
                wrapperTable.eq("STATUS", entry.getKey());
                wrapperTable.groupBy("VERSION,STATUS");
                wrapperTable.select("count(1) AS COUNT,STATUS");
                wrapperTable.orderByAsc("STATUS");
                List<Table> tableList = tableService.list(wrapperTable);
                if (CollectionUtils.isEmpty(tableList)) {
                    integerList.add(0);
                    continue;
                }
                for (Table table : tableList) {
                    if (null == tableList) {
                        integerList.add(0);
                        continue;
                    }
                    integerList.add(table.getCount());
                }
            }
            series.put("data", integerList);
            series.put("name", entry.getValue());
            list.add(series);
        }
        map.put("series", list);
        map.put("categories", categories);
        return map;
    }


    /**
     * 统计脚本审核相关信息
     * 按照当前版本已审核，未审核， 审核退回的统计
     *
     * @return
     * @throws Exception
     */
    private Map<String, Object> getSqlScriptMsg() throws Exception {
        Map<String, Object> map = new HashMap<>();
        //状态
        Map<String, String> mapStatus = dicValueService.getCacheDicValueMap(ReportWorldEnum.REPORT_STATUS.getCode());
        //状态
        if (CollectionUtils.isEmpty(mapStatus)) {
            return null;
        }
        List<Dicvalue> dicvalueList = this.getVersionList();
        if (CollectionUtils.isEmpty(dicvalueList)) {
            return null;
        }
        List<String> categories = new ArrayList<>(); // 所有的状态类型
        for (Dicvalue dicvalue : dicvalueList) {  // 获取每个版本的数据信息
            if (null == dicvalue) {
                continue;
            }
            categories.add(dicvalue.getValue());
        }
        List<Map<String, Object>> list = new ArrayList<>();
        lable:
        for (Map.Entry<String, String> entry : mapStatus.entrySet()) {
            Map<String, Object> series = new HashMap<>(); //  所有状态
            List<Integer> integerList = new ArrayList<>(); //每个状态对应的个数集合
            for (Dicvalue dicvalue : dicvalueList) {  // 获取每个版本的数据信息
                if (null == dicvalue) {
                    continue;
                }
                QueryWrapper<SqlScript> wrapperSqlScript = new QueryWrapper<>();
                wrapperSqlScript.eq("VERSION", dicvalue.getValue());
                wrapperSqlScript.eq("STATUS", entry.getKey());
                wrapperSqlScript.groupBy("VERSION,STATUS");
                wrapperSqlScript.select("count(1) AS COUNT,STATUS");
                wrapperSqlScript.orderByAsc("STATUS");
                List<SqlScript> scriptList = sqlScriptService.list(wrapperSqlScript);
                if (CollectionUtils.isEmpty(scriptList)) {
                    integerList.add(0);
                    continue;
                }
                for (SqlScript sqlScript : scriptList) {
                    if (null == scriptList) {
                        integerList.add(0);
                        continue;
                    }
                    integerList.add(sqlScript.getCount());
                }
            }
            series.put("data", integerList);
            series.put("name", entry.getValue());
            list.add(series);
        }
        map.put("series", list);
        map.put("categories", categories);
        return map;
    }


    /**
     * 获取前台图标展示的版本号
     *
     * @return
     */
    private List<Dicvalue> getVersionList() throws Exception {
        List<Dicvalue> dicvalueList = dicValueService.getCacheDicValueList(ReportWorldEnum.REPORT_VERSION.getCode());
        if (CollectionUtils.isEmpty(dicvalueList)) {
            return null;
        }
        List<Dicvalue> dicvalueListnew = new ArrayList<>();
        for (int i = dicvalueList.size() - 1; i >= 0; i--) {  // 获取每个版本的数据信息

            if (dicvalueList.size() > 5) { //超过5个版本，只显示5个
                if (dicvalueList.size() - i == 6) {
                    break;
                }
            }
            dicvalueListnew.add(dicvalueList.get(i));
        }
        return dicvalueListnew;
    }

}
